package com.pur.zsms.supplier.service.impl;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import net.sf.cglib.beans.BeanCopier;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.DeleteFileUtil;
import com.pur.zsms.common.util.PubFun;
import com.pur.zsms.common.vo.CommonType;
import com.pur.zsms.dao.IDistributorDao;
import com.pur.zsms.matermanage.mater.dao.IMaterDao;
import com.pur.zsms.matermanage.mater.po.MaterLevelCblPo;
import com.pur.zsms.matermanage.mater.po.MaterLevelPo;
import com.pur.zsms.matermanage.mater.po.MaterPo;
import com.pur.zsms.matermanage.mater.service.MaterComputeService;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.matermanage.mater.vo.MaterVoForCostSupplier;
import com.pur.zsms.matermanage.mater.vo.OrderDistributorMaterRealVo;
import com.pur.zsms.ordercustommanage.dao.ISelectFoodDao;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterRealPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierPo;
import com.pur.zsms.ordercustommanage.vo.OrderSupplierMaterVo;
import com.pur.zsms.ordercustommanage.vo.SelectFoodVo;
import com.pur.zsms.pmsnmanage.user.dao.IUserDao;
import com.pur.zsms.pmsnmanage.user.po.UserRolePo;
import com.pur.zsms.pmsnmanage.user.service.IUserService;
import com.pur.zsms.pmsnmanage.user.service.impl.UserServiceImpl;
import com.pur.zsms.shopmanage.dao.IShopDao;
import com.pur.zsms.shopmanage.po.ShopPo;
import com.pur.zsms.storemanage.dao.IStoreDao;
import com.pur.zsms.storemanage.po.StoreMaterPo;
import com.pur.zsms.storemanage.po.StorePo;
import com.pur.zsms.supplier.dao.SupplierDao;
import com.pur.zsms.supplier.dao.SupplierOrderDao;
import com.pur.zsms.supplier.po.ShopSupplierTypePo;
import com.pur.zsms.supplier.po.SupplierDistributePo;
import com.pur.zsms.supplier.po.SupplierPo;
import com.pur.zsms.supplier.po.SupplierQualificationPo;
import com.pur.zsms.supplier.po.SupplierTypePo;
import com.pur.zsms.supplier.po.SupplierUserPo;
import com.pur.zsms.supplier.service.ISupplierPurchasesService;
import com.pur.zsms.supplier.service.ISupplierService;
import com.pur.zsms.supplier.vo.ShopSupplierVo;
import com.pur.zsms.supplier.vo.SupplierDistributeVo;
import com.pur.zsms.supplier.vo.SupplierOrderCustVo;
import com.pur.zsms.supplier.vo.SupplierQualificationVo;
import com.pur.zsms.supplier.vo.SupplierVo;
import com.pur.zsms.systemsetting.datadictionary.po.Code;
import com.pur.zsms.systemsetting.datadictionary.vo.CodeVo;

@Service("supplierPurchasesService")
public class SupplierPurchasesServiceImpl implements ISupplierPurchasesService {
	String msg = "";

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Resource(name = "supplierDao")
	private SupplierDao supplierDao;

	@Resource(name = "materDao")
	private IMaterDao materDaoImpl;

	@Resource(name = "storeDao")
	private IStoreDao storeDao;

	@Resource(name = "selectFoodDao")
	private ISelectFoodDao selectFoodDaoImpl;

	@Resource(name = "distributorDao")
	private IDistributorDao distributorDao;

	public MaterVo getMaterPoForPurchases(int materId, int storeHouseId, String date, int supId) throws GenericException {
		MaterVo materVo = new MaterVo();
		materVo.setMaterId(materId);
		// 取得 材料基本属性
		MaterPo materPo = materDaoImpl.getMaterPo(materVo).get(0);
		BeanCopier copier = BeanCopier.create(MaterPo.class, MaterVo.class, false);
		copier.copy(materPo, materVo, null);
		// 取得库存信息
		List<StoreMaterPo> tStoreMaterPoList = storeDao.getStoreMaterListbyHoustIdMaterId(storeHouseId, materId);
		if (tStoreMaterPoList != null && tStoreMaterPoList.size() > 0) {
			StoreMaterPo tStoreMaterPo = tStoreMaterPoList.get(0);
			materVo.setMaterStoreNumber(tStoreMaterPo.getMaterNum());
		} else {
			materVo.setMaterStoreNumber(new BigDecimal(0));
		}
		// 取得取得客户需求量
		List<Object> list = selectFoodDaoImpl.getOrderSupplierMaterPoByMaterIdAndOrderDateAndSupId(materId, date, supId);
		if (list != null && list.size() > 0 && list.get(0) != null && !list.get(0).equals("null")) {
			materVo.setMaterWeightNeed(list.get(0).toString());
		} else {
			materVo.setMaterWeightNeed("0");
		}
		return materVo;
	}

	public List<OrderDistributorMaterRealVo> changeDistributorOrder(OrderDistributorMaterRealVo orderDistributorMaterRealVo, int storeHouseId,
			int supId) throws GenericException {
		// 取得取得客户需求量
		List<Object[]> list = distributorDao.getOrderSupplierMaterForDistributor(orderDistributorMaterRealVo.getOrdMaterId(),
				orderDistributorMaterRealVo.getOrdMaterLevel(), orderDistributorMaterRealVo.getOrderDate(), supId);
		List<OrderDistributorMaterRealVo> orderDistributorMaterRealVoList = new ArrayList<OrderDistributorMaterRealVo>();
		if (list != null && list.size() > 0) {
			for (Object[] object : list) {
				OrderDistributorMaterRealVo tOrderDistributorMaterRealVo = new OrderDistributorMaterRealVo();
				tOrderDistributorMaterRealVo.setOrdMaterId(Integer.valueOf(object[0].toString()));
				tOrderDistributorMaterRealVo.setMaterName(object[1].toString());
				tOrderDistributorMaterRealVo.setMaterType(Integer.valueOf(object[2].toString()));
				tOrderDistributorMaterRealVo.setUnit(object[3].toString());
				tOrderDistributorMaterRealVo.setBrands(object[4].toString());
				tOrderDistributorMaterRealVo.setSpecifications(object[5].toString());
				tOrderDistributorMaterRealVo.setOrdMaterWeight(new BigDecimal(object[6].toString()));
				tOrderDistributorMaterRealVo.setCode(object[7].toString());
				tOrderDistributorMaterRealVo.setOrdMaterLevel(Integer.valueOf(object[8].toString()));
				orderDistributorMaterRealVoList.add(tOrderDistributorMaterRealVo);
			}
		}

		return orderDistributorMaterRealVoList;
	}
}
